package com.thomasgustavo.meugood;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;

public class MainActivity extends Activity {
	
	public final static String NRO = "com.thomasgustavo.meugood.NRO";
	public final static String CPF = "com.thomasgustavo.meugood.CPF";
	public final static String DATA = "com.thomasgustavo.meugood.DATA";
	
	private Preferencias preferencias;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		preferencias = new Preferencias(getPreferences(Context.MODE_PRIVATE));
		
		loadCartoesSalvos();
		
		Login.destroy();
	}

	private void loadCartoesSalvos() {
		Spinner spinner = (Spinner) findViewById(R.id.spinner1);
		
		List<Cartao> lstCartoes = preferencias.lst();
		
		List<String> lstSpinner = new ArrayList<String>();
		
		for( Cartao cartao : lstCartoes ){
			lstSpinner.add(cartao.getNro());
		}
		
		SpinnerAdapter adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, lstSpinner);
		spinner.setAdapter(adapter);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

	public void verSaldo(View view){
		Intent intent = new Intent(this, ViewActivity.class);
		
		Spinner spinner = (Spinner) findViewById(R.id.spinner1);
		String nroCartao = (String) spinner.getSelectedItem();
		
		DatePicker data = (DatePicker) findViewById(R.id.datePicker1);
		
		Cartao cartao = preferencias.get(nroCartao);
		
		intent.putExtra(NRO, cartao.getNro());
		intent.putExtra(CPF, cartao.getCpf());
		intent.putExtra(DATA, getData(data.getMonth() + 1, data.getYear()));
		
		startActivity(intent);
	}
	
	private String getData(int month, int year) {
		String result = year + "";
		if( month < 10 ){
			result += "0" + month;
		} else {
			result += month;
		}
		Date hoje = new Date();
		if( hoje.getYear() + 1900 == year && hoje.getMonth() + 1 == month ){
			result += "01|X";
		} else {
			result += "01|S";
		}
		return result;
	}
	
	public void addCartao(View view){
		EditText etNroCartao = (EditText) findViewById(R.id.editText1);
		String nroCartao = etNroCartao.getText().toString();
		
		EditText etCpf = (EditText) findViewById(R.id.editText2);
		String cpf = etCpf.getText().toString();
		
		preferencias.add(new Cartao(nroCartao, cpf));
		
		loadCartoesSalvos();
	}
	
	public void delCartao(View view){
		Spinner spinner = (Spinner) findViewById(R.id.spinner1);
		String nroCartao = (String) spinner.getSelectedItem();
		
		preferencias.del(nroCartao);
		
		loadCartoesSalvos();
	}
}
